﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Form_YOLO_NET8
{
    public class LabelToYolo
    {
        public List<string> GetClass(string jsonDir) 
        {
            // 存放json文件的列表
            List<string> listLabels = new List<string>();
            List<string> classNames = new List<string>();


            // 1.获取目录下所有的labelme标注好的Json文件，存入列表中
            foreach (var file in Directory.GetFiles(jsonDir, "*.json"))
            {
                listLabels.Add(file);
            }

            foreach (var label in listLabels)
            {
                using (StreamReader sr = new StreamReader(label))
                {
                    string jsonContent = sr.ReadToEnd();
                    JObject fileIn = JObject.Parse(jsonContent);
                    JArray shapes = (JArray)fileIn["shapes"];
                  
                    foreach (var shape in shapes)
                    {
                        List<string> lineContent = new List<string>();
                        string labelName = shape["label"].ToString();
                        if (!classNames.Contains(labelName))
                        {
                            classNames.Add(labelName);
                        }
                    }

                            
                }
            }

            return classNames;

        }




        /// <summary>
        /// Json文件格式转换为Yolo的TXT文件
        /// </summary>
        /// <param name="classNames">标签种类</param>
        /// <param name="jsonDir">原始文件夹地址</param>
        /// <param name="labelsDir">转换后文件夹地址 </param>
        public void Labelme2YoloSeg(List<string> classNames, string jsonDir, string labelsDir)
        {
            // 存放json文件的列表
            List<string> listLabels = new List<string>();

            // 0.创建保存转换结果的文件夹
            if (!Directory.Exists(labelsDir))
            {
                Directory.CreateDirectory(labelsDir);
            }

            // 1.获取目录下所有的labelme标注好的Json文件，存入列表中
            foreach (var file in Directory.GetFiles(jsonDir, "*.json"))
            {
                listLabels.Add(file);
            }

            foreach (var label in listLabels)
            {
                using (StreamReader sr = new StreamReader(label))
                {
                    string jsonContent = sr.ReadToEnd();
                    JObject fileIn = JObject.Parse(jsonContent);
                    JArray shapes = (JArray)fileIn["shapes"];
                    Console.WriteLine(label);

                    string txtFilename = Path.GetFileName(label).Replace(".json", ".txt");
                    string txtPath = Path.Combine(labelsDir, txtFilename);

                    using (StreamWriter sw = new StreamWriter(txtPath))
                    {
                        foreach (var shape in shapes)
                        {
                            List<string> lineContent = new List<string>();
                            string labelName = shape["label"].ToString();
                            int classIndex = classNames.IndexOf(labelName);
                            lineContent.Add(classIndex.ToString());

                            // 添加坐标信息
                            JArray points = (JArray)shape["points"];
                            double imageWidth = (double)fileIn["imageWidth"];
                            double imageHeight = (double)fileIn["imageHeight"];

                            foreach (var point in points)
                            {
                                double x = (double)point[0] / imageWidth;
                                double y = (double)point[1] / imageHeight;
                                lineContent.Add(x.ToString());
                                lineContent.Add(y.ToString());
                            }

                            // 使用空格连接列表中的所有元素，并写入文件
                            sw.WriteLine(string.Join(" ", lineContent));
                        }
                    }
                }
            }
        }









    }



    
}
